﻿using CefSharp;
using CefSharp.WinForms;
using System;
using System.Text;
using System.Windows.Forms;

namespace web_frame
{
    public partial class Main : Form
    {
        private ChromiumWebBrowser WebBrowser;

        public Main()
        {
            InitializeComponent();
        }

        private void Main_Load(object sender, EventArgs e)
        {
            var settings = new CefSettings()
            {
                Locale = "zh-CN",
                AcceptLanguageList = "zh-CN",
                PersistSessionCookies = true,
                MultiThreadedMessageLoop = true,
                ExternalMessagePump = false,
                LogSeverity = LogSeverity.Disable,
                UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36",
                CachePath = AppDomain.CurrentDomain.BaseDirectory + "caches\\",
            };
            Cef.Initialize(settings);

            WebBrowser = new ChromiumWebBrowser("https://www.tmall.com/")
            {
                // 填充整个父控件
                Dock = DockStyle.Fill,
                //自身窗口打开链接
                LifeSpanHandler = new OpenPageSelf(),
                //相应按键
                KeyboardHandler = new CEFKeyBoardHander(),
                //屏蔽右键菜单 
                MenuHandler = new MenuHandler(),

            };  
            WebBrowser.AddressChanged += OnBrowserAddressChanged;        //地址改变
            WebBrowser.TitleChanged += OnBrowserTitleChanged;            //标题改变
            WebBrowser.FrameLoadEnd += OnBrowserFrameLoadEnd;            //载入完成

            MyRequestHandler handler = new MyRequestHandler();
            handler.ResponseData += ResponseData;
            WebBrowser.RequestHandler = handler;          //请求拦截

            // 添加到窗口的控件列表中
            this.Panel1.Controls.Add(WebBrowser);
        }

        private void OnBrowserAddressChanged(object sender, AddressChangedEventArgs args)
        {
            this.BeginInvoke(new MethodInvoker(() => { TextBox1.Text = args.Address; }));
        }

        private void OnBrowserTitleChanged(object sender, TitleChangedEventArgs args)
        {
            this.BeginInvoke(new MethodInvoker(() => { this.Text = args.Title; }));
        }

        private void OnBrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
        {

        }

        private void ResponseData(string url, string Charset, string MimeType, int StatusCode, string StatusText, byte[] data)
        {
            Console.WriteLine("****************Response_Start****************");
            Console.WriteLine(url);
            Console.WriteLine(Charset);
            Console.WriteLine(MimeType);
            Console.WriteLine(StatusCode);
            Console.WriteLine(StatusText);
            Console.WriteLine(Encoding.Default.GetString(data));
            Console.WriteLine("****************Response_End****************");
        }

        private void Main_FormClosed(object sender, FormClosedEventArgs e)
        {
            // 结束时要销毁
            Cef.Shutdown();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            WebBrowser.Back();
        }

        private void Button2_Click(object sender, EventArgs e)
        {
            WebBrowser.Forward();
        }

        private void Button3_Click(object sender, EventArgs e)
        {
            WebBrowser.Reload();
        }

        private void Button4_Click(object sender, EventArgs e)
        {
            WebBrowser.LoadUrl("https://www.tmall.com");
        }

        private void TextBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                WebBrowser.LoadUrl(TextBox1.Text);
            }
        }

        private void Button5_Click(object sender, EventArgs e)
        {
            WebBrowser.EvaluateScriptAsync("window.alert(\"我是脚本啊！！\");");
        }
    }
}
